import React, { Component } from 'react';
import { Select } from 'antd';
import { isObject, isEmpty, map } from 'lodash';

const { Option } = Select;

class EditableComboBox extends Component {
    constructor(props) {
        super(props);
    }

    static getDerivedStateFromProps(nextProps) {
        if ('value' in nextProps) {
            return {
                value: nextProps.value,
            };
        }
    }

    componentDidMount() {}

    handleChange(value) {
        if (!('value' in this.props)) {
            this.setState({value});
        }
        this.triggerChange(value);
    }

    triggerChange = (changedValue) => {
        // Should provide an event to pass value to Form.
        const onChange = this.props.onChange;
        if (onChange) {
            onChange(changedValue);
        }
    };

    render() {
        const { nativeProps,compData } = this.props;
        //console.log(nativeProps);
        console.log();
        return (
            <Select
                    value={this.state.value}
                    showSearch
                    onChange={this.handleChange.bind(this)}
                    style={{ minWidth: '100px' }}
                    {...nativeProps}>
                <Option value="">请选择</Option>
                {map(compData, (opt, oK) => (
                    <Option key={oK} value={opt.code}>
                        {opt.name}
                    </Option>
                ))}
            </Select>
        );
    }
}

export default EditableComboBox;
